package com.suanfa.tianxun;

/**
 * @author tianxun
 * @date 2023/12/13 10:18
 * @desc 二分查找
 */
public class BinarySearch {
    public static void main(String[] args) {
        int[] arr = {15, 27, 33, 49, 88, 101, 700};
        int n = 700;
        int low = 0;
        int high = arr.length-1;
        test(low,high,n,arr);
    }

    private static void test(int low, int high, int n,int[] arr) {
        int mid = (low + high) / 2;
        if(n >arr[mid]){
            low = mid+1;
            if(low == arr.length){
                System.out.println("此数组中没有指定查找的元素n:"+n);
                return;
            }
            test(low,high,n,arr);
        }else if(n <arr[mid]){
            high = mid-1;
            if(high == -1){
                System.out.println("此数组中没有指定查找的元素n:"+n);
                return;
            }
            test(low,high,n,arr);
        }else if(n == arr[mid]){
            System.out.println("n:"+n+"在数组中的位置是"+mid);
        }
    }
}
